<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="col-sm-12 page-header custom-page-header">
	<h1><spring:message code="menu.configration.cdt"/></h1>
	<small><spring:message code="menu.configration.cdt.desc"/></small>
</div>
<form:form modelAttribute="cDTEntityModel" role="form" cssClass="form-horizontal" name="cDTEntityModel">
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
	<form:input type="hidden" path="userTypeId" />
	<div class="form-group">
		<form:label path="userTypeName" cssClass="col-sm-2 control-label"><spring:message code="configration.cdt.userTypeName"/></form:label>
		<div class="col-sm-10"><form:input path="userTypeName" cssClass="form-control"/></div>
	</div>
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="configration.cdt.columns"/></label>
		<div class="rows text-right col-sm-10"><button type="button" class="btn btn-primary addRows hideBtn"><spring:message code="button.add.variable" /></button>
			<table class="table table-hover usertypeTable topmargin20">
				<thead>
					<tr>
						<th class="col-sm-3"><spring:message code="configration.cdt.columns.name"/></th>
						<th class="col-sm-6"><spring:message code="configration.cdt.columns.type"/></th>
						<th class="col-sm-2"><spring:message code="configration.cdt.columns.default"/></th>
						<th class="col-sm-1"></th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${cDTEntityModel.rows }" var="row" varStatus="status">
						<tr>
							<td class="col-sm-3">
								<input type="text" data-prefix="rows[" data-surfix="].columnName"  name="rows[${status.index }].columnName" class="form-control" value="${row.columnName }"/>
							</td>
							<td class="col-sm-6">
								<div class="col-sm-3">
								<select name="rows[${status.index }].columnType" data-prefix="rows[" data-surfix="].columnType" class="usertypeColumnTypeSelect form-control">
									<option value=""><spring:message code="select.seleted.message"/></option>
									<option value="String" <c:if test="${row.columnType eq 'String' }">selected="selected"</c:if>>String</option>
									<option value="Integer" <c:if test="${row.columnType eq 'Integer' }">selected="selected"</c:if>>Integer</option>
									<option value="Double" <c:if test="${row.columnType eq 'Double' }">selected="selected"</c:if>>Double</option>
									<option value="Option" <c:if test="${row.columnType eq 'Option' }">selected="selected"</c:if>>Option</option>
								</select>
								</div>
								<div class="col-sm-9">
								<spring:message code="configration.cdt.columns.typeoption.desc" var="msgUserTypeOptionDesc"/>
								<input type="text" data-prefix="rows[" data-surfix="].columnTypeOption" class="hiddenObject form-control" placeholder="${msgUserTypeOptionDesc }" name="rows[${status.index }].columnTypeOption" value="${row.columnTypeOption }"/>
								</div>
							</td>
							<td class="col-sm-2"><input type="text" data-prefix="rows[" data-surfix="].defaultValue" class="form-control" name="rows[${status.index }].defaultValue" value="${row.defaultValue }"/></td>
							<td class="col-sm-1"><button class="btn btn-warning deleteRows"><spring:message code="button.delete"/></button></td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
	</div>
	<div class="form-group">
		<div class="col-sm-12 text-right">
			<button class="btn btn-default btn_link" data-link="./lists" type="button"><spring:message code="button.list"/></button>
			<c:if test="${fn:contains(buttons , Authorities.UPDATE)}">
			<button class="btn btn-primary updateBtn" type="button"><spring:message code="button.edit"/></button>
			<button class="btn btn-success updateSubmitBtn hideBtn" type="button"><spring:message code="button.edit"/></button>
			</c:if>
			<c:if test="${fn:contains(buttons , Authorities.REMOVE)}">
			<button class="btn btn-primary removeBtn" type="button"><spring:message code="button.delete"/></button>
			</c:if>
		</div>
	</div>
</form:form>
<table class="table table-hover usertypeTempTable hiddenObject">
	<tr>
		<td class="col-sm-3">
			<input type="text" data-prefix="rows[" data-surfix="].columnName"  class="form-control"/>
		</td>
		<td class="col-sm-6">
			<div class="col-sm-3">
			<select name="" data-prefix="rows[" data-surfix="].columnType" class="usertypeColumnTypeSelect form-control">
				<option value=""><spring:message code="select.seleted.message"/></option>
				<option value="String">String</option>
				<option value="Integer">Integer</option>
				<option value="Double">Double</option>
				<option value="Option">Option</option>
			</select>
			</div>
			<div class="col-sm-9">
			<spring:message code="configration.cdt.columns.typeoption.desc" var="msgUserTypeOptionDesc"/>
			<input type="text" data-prefix="rows[" data-surfix="].columnTypeOption" class="hiddenObject form-control" placeholder="${msgUserTypeOptionDesc }"/>
			</div>
		</td>
		<td class="col-sm-2"><input type="text" data-prefix="rows[" data-surfix="].defaultValue" class="form-control"/></td>
		<td class="col-sm-1"><button class="btn btn-warning deleteRows"><spring:message code="button.delete"/></button></td>
	</tr>
</table>
<script type="text/javascript"><!--
function doFormNameSetting() {
	var targetTable = $(".usertypeTable tbody");
	var targetTr = targetTable.find("tr");
	$.each(targetTr, function(index, item) {
		var currentIndex = $(this).index();
		var targetTd = $(this).find(":input");
		$.each(targetTd, function(index, item) {
			$(this).attr("name", $(this).data("prefix") + currentIndex + $(this).data("surfix"));
		});
	});
}

function initUserTypeColumnTypeSelect() {
	$(".usertypeColumnTypeSelect").each(function() {
		switch($(this).find(":selected").val()) {
		case "String":
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Integer":
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Double":
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Option":
			$(this).parent().parent().find(".hiddenObject").show();
			break;
		default:
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		}
		doFormNameSetting();
	});
}

$(document).ready(function() {
	$(".hiddenObject").hide();
	$(".hideBtn").hide();
	$(".form-horizontal").find(":input").attr("disabled","true");
	$(".form-horizontal").find(".btn").removeAttr("disabled");
	$(".form-horizontal").find(".deleteRows").attr("disabled","true");
	
	initUserTypeColumnTypeSelect();
	
	/* Row 추가 */
	$(document).on("click", ".addRows", function() {
		var copyTr = $(".usertypeTempTable").find("tr").clone();
		$(".usertypeTable").append(copyTr);
		doFormNameSetting();
	});
	
	/* Row 삭제 */
	$(document).on("click", ".deleteRows", function() {
		$(this).parent().parent().remove();
		doFormNameSetting();
	});
	
	$(document).on("change", ".usertypeColumnTypeSelect", function() {
		switch($(this).find(":selected").val()) {
		case "String":
			$(this).parent().parent().find(".hiddenObject").val("");
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Integer":
			$(this).parent().parent().find(".hiddenObject").val("");
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Double":
			$(this).parent().parent().find(".hiddenObject").val("");
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		case "Option":
			$(this).parent().parent().find(".hiddenObject").val("");
			$(this).parent().parent().find(".hiddenObject").show();
			break;
		default:
			$(this).parent().parent().find(".hiddenObject").val("");
			$(this).parent().parent().find(".hiddenObject").hide();
			break;
		}
		doResetInpuNames();
	}).on("click", ".submitBtn", function() {
		
	}).on("click", ".updateBtn", function() {
		$(this).hide();
		$(".hideBtn").show();
		$(".form-horizontal").find(":input").removeAttr("disabled");
	}).on("click", ".updateSubmitBtn", function() {
		bootbox.confirm("<spring:message code='common.update.confirm'/>",function(result) {
			if ( result ) {
				Loading.show();
				$.post("./edit", $("#cDTEntityModel").serialize(), function(res) {
					
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>"); /* 서버 통신 오류 */
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.update.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.update.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});	
			}
		});
	}).on("click", ".removeBtn", function() {
		bootbox.confirm("<spring:message code='common.remove.confirm'/>",function(result) {
			if ( result ) {
				Loading.show();
				$(".form-horizontal").find(":input").removeAttr("disabled");
				$.post("./remove", $("#cDTEntityModel").serialize(), function(res) {
					
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>"); /* 서버 통신 오류 */
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.remove.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.remove.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});	
			}
		});
	});
});
//--></script>